package com.itheima.web.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.Freeze;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.FreezeService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class FreezeManager {
    @DubboReference
    UserInfoService userInfoService;
    @DubboReference
    FreezeService freezeService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //冻结账户
    public ResponseEntity freezeUser(Freeze freeze) {
        //判断用户冻结时间
        int time = 36500;
        if (freeze.getFreezingTime() == 1) {
            time = 3;
        } else if (freeze.getFreezingTime() == 2) {
            time = 7;
        }
        //1.判断用户是否有冻结信息
        Freeze freezeInfo = freezeService.findFreezeUserByUserId(freeze.getUserId());
        UserInfo userInfo = userInfoService.findByUserId(freeze.getUserId());
        if (freezeInfo == null) {
            //1.1更新userInfo表中用户states信息
            userInfo.setUserStatus(2);
            //1.2 调用方法保存
            userInfoService.update(userInfo);
            //1.3 调用FreezeService执行保存操作
            freezeService.saveFreeze(freeze);
            //1.4 存入Redis中设置失效时间 天为单位
            stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.USER_FREEZE_RANGE, freeze.getUserId(), freeze.getFreezingRange()), freeze.getFreezingTime().toString(),
                    Duration.ofDays(time));
        } else {
            //2.1 更改userInfo表中的数据
            userInfo.setUserStatus(2);
            userInfoService.update(userInfo);
            //2.2 移除Redis中原来的冻结信息
            stringRedisTemplate.delete(StrUtil.format(ConstantUtil.USER_FREEZE_RANGE, freezeInfo.getUserId(), freezeInfo.getFreezingRange()));
            //2.3 更新冻结表freeze中的数据
            freezeService.updateFreeze(freeze);
            //2.4 更新redis
            //设置失效时间 天为单位
            stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.USER_FREEZE_RANGE, freeze.getUserId(), freeze.getFreezingRange()), freeze.getFreezingTime().toString(),
                    Duration.ofDays(time));
        }
        String message = "操作成功！";
        return ResponseEntity.ok(message);
    }

    //手动解冻账户
    public ResponseEntity unfreezeUser(Freeze freeze) {
        //1.1 获取用户ID,更新user表信息
        UserInfo userInfo = userInfoService.findByUserId(freeze.getUserId());
        userInfo.setUserStatus(1);
        userInfoService.update(userInfo);
        //1.2 查询用户原来的冻结信息
        Freeze freezeInfo = freezeService.findFreezeUserByUserId(freeze.getUserId());
        //1.3 移除Redis中原来的冻结信息
        Boolean delete = stringRedisTemplate.delete(StrUtil.format(ConstantUtil.USER_FREEZE_RANGE, freeze.getUserId(), freezeInfo.getFreezingRange()));
        //1.4 更新freeze表数据
        freezeInfo.setReasonsForThawing(freeze.getFrozenRemarks());
        freezeService.updateFreeze(freezeInfo);
        String message = "操作成功！";
        return ResponseEntity.ok(message);
    }

}
